package com.platform.qujia.utils.weixinapp;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

/****
 * <Title> HttpRequestUtil </Title>
 * <Description> 发送https请求获取数据工具类 </Description>
 * @author JK
 * @date 2019年11月14日
 */
public class HttpRequestUtil {

    private static final Logger log = LoggerFactory.getLogger(HttpRequestUtil.class);
    /*
     * 发送https请求
     * @param requestUrl    // 请求地址
     * @param requestMethod     //  请求方式(POST, GET)
     * @param outPutStr         //提交的数据
     * @return  jsonObject (通过JSONObject.get(key)的方式获取json的对象)
     */
    public static JSONObject getHttpsRequestData(String requestUrl, String requestMethod, String outPutStr){
        JSONObject jsonObject = null;
        try {
            //创建SSLContext对象
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            //指定信任管理器
            TrustManager[] trustManagers = { new MyX509TrustManager()};
            sslContext.init(null, trustManagers, new SecureRandom());
            //从SSLContext获取SSLSocketFactory对象
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();

            httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setDoInput(true);
            httpsURLConnection.setUseCaches(false);
            //设置请求方式
            httpsURLConnection.setRequestMethod(requestMethod);

            //不为null时想输出流写数据
            if(null != outPutStr){
                OutputStream outputStream = httpsURLConnection.getOutputStream();
                outputStream.write(outPutStr.getBytes("UTF-8"));
                outputStream.close();
            }
            //从输入流读取返回结果
            InputStream inputStream = httpsURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer stringBuffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null){
                stringBuffer.append(str);
            }
            //关闭流
            bufferedReader.close();
            inputStreamReader.close();
            //inputStream = null;
            inputStream.close();
            httpsURLConnection.disconnect();
            jsonObject = JSONObject.fromObject(stringBuffer.toString());
            //System.out.println(jsonObject);
        } catch (NoSuchAlgorithmException | NoSuchProviderException | KeyManagementException | IOException e) {
            log.error(e.getMessage(),e);
        }
        return jsonObject;
    }

    public static String getAcccessTokenHttpsRequestData(String requestUrl, String requestMethod, String outPutStr){
        String  mmm="";
        try {
            //创建SSLContext对象
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            //指定信任管理器
            TrustManager[] trustManagers = { new MyX509TrustManager()};
            sslContext.init(null, trustManagers, new SecureRandom());
            //从SSLContext获取SSLSocketFactory对象
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();

            httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setDoInput(true);
            httpsURLConnection.setUseCaches(false);
            //设置请求方式
            httpsURLConnection.setRequestMethod(requestMethod);

            //不为null时想输出流写数据
            if(null != outPutStr){
                OutputStream outputStream = httpsURLConnection.getOutputStream();
                outputStream.write(outPutStr.getBytes("UTF-8"));
                outputStream.close();
            }
            //从输入流读取返回结果
            InputStream inputStream = httpsURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer stringBuffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null){
                stringBuffer.append(str);
            }
            //关闭流
            bufferedReader.close();
            inputStreamReader.close();
            //inputStream = null;
            inputStream.close();
            mmm=stringBuffer.toString();
            //System.out.println(jsonObject);
        } catch (NoSuchAlgorithmException | NoSuchProviderException | KeyManagementException | IOException e) {
            log.error(e.getMessage(),e);
        }
        return mmm;
    }


}
